<template>
  <div>
    <el-dialog
      width="500px"
      :title="$t('店铺最大库存编辑')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form @submit.native.prevent ref="formRef" :model="form" :rules="rules">
        <el-form-item :label="$t('数量')" prop="maxStock">
          <el-input v-model="form.maxStock" :placeholder="$t('请输入')" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleSubmit" type="primary">{{ $t('修改') }}</el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateSkuShopSetting } from '@/api/distribution/sku/storeSkuList'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      // 表单参数
      form: {
        maxStock: '',
      },
      // 表单校验
      rules: {
        maxStock: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        Object.keys(this.formData).forEach((key) => (this.form[key] = this.formData[key] || ''))
      }
    },
  },
  methods: {
    getList() {},

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = {
            id: this.formData.id,
            maxStock: this.form.maxStock,
          }
          updateSkuShopSetting(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.cancel()
              this.$emit('getList')
            }
          })
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
